package com.qeebu.SMSManagerClient.smsList;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.qeebu.SMSManagerClient.util.UtilMethod;


import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.View;

/**
 * 从数据库读取单个联系人的短信数据的异步线程
 * @author yongjun
 *
 */
public class ReadPersonSMSAsyncTask extends AsyncTask<String, Integer, List<PersonSMSListItem>>{
    private Context context; 
    
    private static final int DATE = 0;
    private static final int BODY = 1;
    private static String URI_ADRESS = "content://mms-sms/conversations/";
    public static Uri MMSSMS_SINGCONVERSION_ADDRESSES_URI;
    private List<PersonSMSListItem> smsList = new ArrayList<PersonSMSListItem>();

    /*包含日期和短信体两部分*/
    private static final String[] ALL_THREADS_PROJECTION = {
        "date", "body"
    };

    public ReadPersonSMSAsyncTask(Context ctx, String id)
    {
        context = ctx;
        MMSSMS_SINGCONVERSION_ADDRESSES_URI = Uri.parse(URI_ADRESS + id);
    }
    
    @Override
    protected List<PersonSMSListItem> doInBackground(String... arg0)
    {
        Cursor cursor = context.getContentResolver().query(MMSSMS_SINGCONVERSION_ADDRESSES_URI, ALL_THREADS_PROJECTION, null, null, null);
        Calendar calendar = Calendar.getInstance();
        while (cursor.moveToNext()) {
            try {
                PersonSMSListItem smsItem = new PersonSMSListItem();
                long date = cursor.getLong(DATE);
                String body = cursor.getString(BODY);
                //long snippetCS = cursor.getLong(SNIPPET_CS);
                //long read = cursor.getLong(READ);
                //long error = cursor.getLong(ERROR);
                //String num =  cursorNum.getString(0);
                
                Date fDate = new Date(date);
                calendar = Calendar.getInstance();
                calendar.setTime(fDate);
                String sdate = calendar.get(Calendar.YEAR)+"年" + (calendar.get(Calendar.MONTH) + 1) + "月" + 
                                    calendar.get(Calendar.DAY_OF_MONTH) + "日                    ";
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                if(hour<10){
                    sdate += "0";
                }
                sdate += hour+ ":";
                int min = calendar.get(Calendar.MINUTE);
                if(min<10){
                    sdate += "0";
                }
                sdate += min;
                smsItem.setDate(sdate); 

                smsItem.setBody(body);
                smsList.add(smsItem);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
            return smsList;
       }
       
       @Override
       protected void onPostExecute(List<PersonSMSListItem> list) {
           ((PersonSMSListActivity)context).setSmsList(list);
           ((PersonSMSListActivity)context).setAdapter();
           ((PersonSMSListActivity)context).getProgressBar().setVisibility(View.GONE);
       }
}